define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope,$rootScope, T_address_add, GoPage, GoDialog, $stateParams, GoAddress, Http) {
		$scope.scope = new Object();
		$scope.region = '';
		$scope.id = $stateParams.id;
		$scope.input = new Object();
		$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
		$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
		$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		$scope.title = "新增";
		
		//定义绑定
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);

		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function(data) {
			$scope.input.city = null;
			$scope.input.district = null;
			$scope.city_items = data;
			$scope.district_items = '';
			if($scope.is_city == true) {
				for(var i = 0; i < $scope.city_items.length; i++) {
					if($scope.input.city_name == $scope.city_items[i].name) {
						$scope.input.city = $scope.city_items[i];
						break;
					}
				}
				$scope.is_city = false;
			}
		};
		$scope.listen_city = function(data) {
			$scope.input.district = null;
			if($scope.is_district == true) {
				for(var i = 0; i < $scope.district_items.length; i++) {
					if($scope.input.district_name == $scope.district_items[i].name) {
						$scope.input.district = $scope.district_items[i];
						break;
					}
				}
				$scope.is_district = false;
			}
		};
		
		//页面初始化
		$scope.init_data = function() {
			if($scope.id) {
				T_address_add.address_detail($scope.id, function(data) {
					console.log(data)
					$scope.scope = data;
					$scope.init_province(data.region_id);
					$scope.is_city = true;
					$scope.is_district = true;
					$scope.title = "编辑";
					if(data.def_addr) $scope.isShowDefualt = true;
				})
			}
		}
		$scope.init_data(); //必须在定义之后
		
		//初始化接口中的省份
		$scope.init_province = function(data) {
			var arr = new Array();
			arr = data.split('/');
			var i = arr.length;
			if(i == 1) {
				$scope.input.province_name = arr[0].split(':')[0];
			} else
				$scope.input.province_name = arr[0];
			if(i == 2) {
				$scope.input.city_name = arr[1].split(':')[0];
			}
			if(i == 3) {
				$scope.input.city_name = arr[1];
				$scope.input.district_name = arr[2].split(':')[0];
			}
			GoAddress.init_htp($scope, 0, '', function(data) {
				for(var i = 0; i < $scope.province_items.length; i++) {
					if($scope.input.province_name == $scope.province_items[i].name) {
						$scope.input.province = $scope.province_items[i];
						break;
					}
				}
			})
		}
		
		//提交
		$scope.save = function() {
			$scope.get_region();
			if(!$scope.scope.name) {
				GoDialog.toast("请输入收货人名字");
				return
			} else if(!$scope.scope.mobile) {
				GoDialog.toast("请输入手机号码");
				return
			} else if(!$scope.region) {
				GoDialog.toast("请输入所在区域");
				return
			}else if(!$scope.input.district){
				GoDialog.toast("请输入完整所在地信息");
				return
			} else if(!$scope.scope.addr) {
				GoDialog.toast("请输入详细地址");
				return
			}
			if(isNaN($scope.scope.mobile)) {
				GoDialog.toast("手机号码格式错误");
				return
			}
			if($scope.scope.mobile.length != 11) {
				GoDialog.toast("手机号码长度不正确");
				return
			}
			T_address_add.address_save($scope.scope.name, $scope.scope.mobile, $scope.scope.zip, $scope.region, $scope.scope.addr, $scope.id, $scope.scope.def_addr, function(data) {
				if(data.success) {
					GoDialog.toast("保存成功");
					GoPage.go_back(-1); //返回上一页
					//注册了全局广播
					$rootScope.$broadcast('editAddressEvent');
				} else{
					GoDialog.alert("提示",data.msg);
				}
			})
		}

		$scope.get_region = function() {
			if($scope.input.province) {
				$scope.region_name = $scope.input.province.name;
				$scope.region = $scope.region_name + ':' + $scope.input.province.id;
			}
			if($scope.input.city) {
				$scope.region_name += '/' + $scope.input.city.name;
				$scope.region = $scope.region_name + ':' + $scope.input.city.id;
			}
			if($scope.input.district) {
				$scope.region_name += '/' + $scope.input.district.name;
				$scope.region = $scope.region_name + ':' + $scope.input.district.id;
			}
		}
		
		//是否默认
		$scope.handleIsDefualt = function(){
			$scope.scope.def_addr = !$scope.scope.def_addr;
		}
		
		
	}

	controller.$inject = ['$scope','$rootScope', 'T_address_add', 'GoPage', 'GoDialog', '$stateParams', 'GoAddress', 'Http'];

	return app.registerController("address_addctrl",controller);
});